#!/bin/bash
# test different variants of the perf format

PERF=${PERF:-perf}

failed() {
	echo FAILED
	exit 1
}
trap failed ERR 0

set -e
set -x
set -o pipefail
set -E

CMD=${1:-bash -c /bin/true}

[ ! -d data ] && mkdir data

check() { 
	echo -- testing "$@"
	FN=data/perf.$(echo -- "$@" | tr -d '{} ')
	FN=${FN/--/}
	rm -f $FN
	if ! $PERF record -o $FN "$@" -- $CMD ; then
		return
	fi
	if [ $(stat -c %s $FN) == 0 ] ; then
		return
	fi
	python perfdata.py $FN > pdata.txt
	python perfpd.py $FN > ppd.txt

	# XXX check more fields
	AS=$(grep -c SAMPLE pdata.txt)
	BS=$($PERF report -i $FN -D | grep -c "PERF_RECORD_SAMPLE")
	[ $AS -eq $BS ]
}

check
check -c 1000
check -g -c 1000
check -b -c 1000
if ! check -b -g fp -c 1000 ; then
	check -b -g -c 1000
fi
check -P -c 10000
check -T -c 10003
#check -e cycles:S 
# seems to be broken in perf
#check -e '{cycles,branches}:S'

set +e 
#check -d
check --group -e cycles,branches,branch-misses -c 1000
check -e '{cycles,branches},{branch-misses,cache-misses}'  -c 1000
check -e cycles,branches,branch-misses -c 1000

# new kernel
#check -g dwarf

# XXX sw trace points

trap "" ERR 0

echo SUCCEEDED
